package com.polaris.live.ui.user_space.proxy

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ConvertUtils
import com.bumptech.glide.Glide
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.MePhotoAdapter
import com.polaris.live.adapter.user_space.UserBannerAdapter
import com.polaris.live.adapter.user_space.UserSpaceGiftAdapter
import com.polaris.live.adapter.user_space.UserSpaceLabelAdapter
import com.polaris.live.common.R.color
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.AppConst.YES
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.constant.CommonConst.CoverPhoto.COVERPHOTOID
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.constant.PagConst.LIKE_BEFORE_PAG
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.recyclerview.CommonItemDecoration
import com.polaris.live.common.util.LogExt
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.common.util.loading.LoadingUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.ActivitySpaceBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.im.sfs.bean.pusher.UserState
import com.polaris.live.interface_bag.S3UploadCallback
import com.polaris.live.net.userService
import com.polaris.live.popup.SelectImagePopup
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.back_resp.UserOtherGiftVo
import com.polaris.live.resp.back_resp.UserOtherPhotoVo
import com.polaris.live.resp.go_resp.UploadPhotoBean
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.CropUtils
import com.polaris.live.utils.RichCharmImgUtil
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.avatar
import com.polaris.live.utils.image.normal
import com.polaris.live.utils.photo.PicturePreview
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.utils.state.UserStateUtils.UserStatus
import com.polaris.live.viewmodel.UserSpaceViewModel
import com.polaris.live.widget.avatar.FrameImageUtils
import com.polaris.live.widget.bubble.DownLoadFileUtil.loadPag
import com.zhpan.indicator.enums.IndicatorSlideMode
import org.libpag.PAGFile
import org.libpag.PAGScaleMode

/**
 * 个人 他人 中心UI赋值
 *
 * @author Created by 半仙 on 2023/9/22/022 16:05
 */
class UserSpaceUiChangeProxy(
    private val mActivity: AppCompatActivity,
    private val mViewModel: UserSpaceViewModel,
    private val mBinding: ActivitySpaceBinding,
    private val mGiftAdapter: UserSpaceGiftAdapter,
    private val mLabelAdapter: UserSpaceLabelAdapter,
) {

    private val mBannerAdapter: UserBannerAdapter by lazy {
        UserBannerAdapter()
    }

    private val mPhotoAdapter: MePhotoAdapter by lazy {
        MePhotoAdapter()
    }

    /**
     * 再次启动摄像头
     */
    fun againCamera() {
        CropUtils.startCamera(mActivity, { result ->
            if (mPhotoAdapter.data.size <= 6) {
                CropUtils.startCrop(mActivity, result)
            }
        }, {
        })
    }

    /**
     * 显示基础信息
     */
    fun uiInitBasic(userBasicVo: UserBasicVo) {
        mBinding.apply {
            Glide.with(mActivity).load(ImageUtils.imgPathMiddle(userBasicVo.avatar))
                .avatar().into(mBinding.ivAvatar)
            mBinding.ivEdit.visibility = View.GONE
            nickName.text = userBasicVo.nickname
            titleNickName.text = userBasicVo.nickname
            nickName.setNicknameColor(
                userBasicVo.nicknameColor,
                color.color_202028
            )
            titleNickName.setNicknameColor(
                userBasicVo.nicknameColor,
                R.color.color_0b0123
            )
            idView.setId(userBasicVo)
            saSex.setSexAndAge(userBasicVo.sex, userBasicVo.birthday)
            cvCountry.setCountry(userBasicVo.country)
            lvLanguage.setLanguage(userBasicVo.secondLanguage)

            //财富等级
            val richs = userBasicVo.richs
            val commonRich = RichCharmImgUtil.getCommonRichImg(richs)
            val (_, _, richsImage) = RichCharmImgUtil.getRichImg(richs)
            mBinding.richImg.setImageResource(richsImage)
            val (levelBack, _) = commonRich.second to "Lv${commonRich.first}"
            if (richs == 0L) {
                richDetail.visibility = View.GONE
            } else {
                richDetail.text = "$richs"
            }
            if (levelBack == null) {
                if (CommonUtils.isMan(userBasicVo.sex)) level1.visibility =
                    View.GONE else level2.visibility = View.GONE
                richLevel.text = "Lv.0"
            } else {
                if (CommonUtils.isMan(userBasicVo.sex)) level1.setRichsLevel(richs) else level2.setRichsLevel(richs)
                richLevel.text = "Lv.${commonRich.first}"
            }
            mBinding.vipView.setVipLevel(userBasicVo.vipLevel)
            //魅力等级
            val charms = userBasicVo.charms
            val commonCharm = RichCharmImgUtil.getCommonCharmImg(charms)
            val (_, _, charmsImage) = RichCharmImgUtil.getCharmImg(charms)
            mBinding.charmImg.setImageResource(charmsImage)
            val (levelCharmBack, _) = commonCharm.second to "Lv${commonCharm.first}"
            if (charms == 0L) {
                charmDetail.visibility = View.GONE
            } else {
                charmDetail.text = "$charms"
            }
            if (levelCharmBack == null) {
                charmLevel.text = "Lv.0"
                if (CommonUtils.isMan(userBasicVo.sex)) level2.visibility =
                    View.GONE else level1.visibility = View.GONE
            } else {
                if (CommonUtils.isMan(userBasicVo.sex)) level2.setCharmsLevel(charms) else level1.setCharmsLevel(charms)
                charmLevel.text = "Lv.${commonCharm.first}"
            }
        }
    }

    /**
     * 显示控件装饰
     */
    private fun showBackground(infoOtherResp: InfoOtherResp) {
        infoOtherResp.decoration?.userSpace?.let {
            val url = it.substringBefore("?")
            if (url.endsWith(PagConst.PAG)) {
                loadPag(mActivity.lifecycleScope, url) { _, localPath ->
                    showPag(localPath)
                }
            } else {
                mBinding.spaceImg.visibility = BottomPopupView.VISIBLE
                Glide.with(mActivity)
                    .load(ImageUtils.imgPath(url))
                    .centerCrop()
                    .into(mBinding.spaceImg)

            }
        }
    }

    private fun showPag(path: String) {
        mBinding.spacePag.visibility = View.VISIBLE
        mBinding.spacePag.apply {
            composition = PAGFile.Load(path)
            setScaleMode(PAGScaleMode.Zoom)
            setRepeatCount(-1)
            if (!isPlaying) {
                play()
            }
        }
    }

    /**
     * 显示头像
     */
    private fun showAvatar(infoOtherResp: InfoOtherResp) {
        //是自己且没修改过头像显示默认
        val isShowAvatar =
            UserManager.getUserId() == infoOtherResp.userId && UserManager.getUser().avatarModified == AppConst.NO
        if (isShowAvatar) {
            Glide.with(mActivity)
                .load(R.drawable.self_default_avatar)
                .avatar()
                .into(mBinding.ivAvatar)
            mBinding.ivEdit.visibility = View.VISIBLE
        } else {
            Glide.with(mActivity)
                .load(ImageUtils.imgPathMiddle(infoOtherResp.avatar))
                .avatar()
                .into(mBinding.ivAvatar)
            mBinding.ivEdit.visibility = View.GONE
        }
        mBinding.avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(infoOtherResp.decoration?.avatarFrame))
        infoOtherResp.decoration?.avatarFrame?.run {
            val lp = mBinding.relTitle.layoutParams as ConstraintLayout.LayoutParams
            lp.marginStart = 2.dp
            mBinding.relTitle.layoutParams = lp
            mBinding.ivAvatar.strokeWidth = 0f
        }
    }

    fun uiInit(infoOtherResp: InfoOtherResp) {
        mBinding.apply {
            showAvatar(infoOtherResp)
            showBackground(infoOtherResp)
            nickName.text = infoOtherResp.nickname
            titleNickName.text = infoOtherResp.nickname
            nickName.setNicknameColor(
                infoOtherResp.decoration?.nicknameColor,
                color.color_202028
            )
            titleNickName.setNicknameColor(
                infoOtherResp.decoration?.nicknameColor,
                R.color.color_0b0123
            )
            idView.setId(infoOtherResp)

            if (infoOtherResp.flag.official == 1L && UserManager.getUser().userId != infoOtherResp.userId) {
                ivOfficial.visibility = View.VISIBLE
            } else {
                ivOfficial.visibility = View.GONE
            }

            saSex.setSexAndAge(infoOtherResp.sex, infoOtherResp.birthday)

            cvCountry.setCountry(infoOtherResp.profile.country)

            lvLanguage.setLanguage(infoOtherResp.profile.secondLanguage)

            //财富等级
            val commonRich = RichCharmImgUtil.getCommonRichImg(infoOtherResp.account.richs)
            val (_, _, richsImage) = RichCharmImgUtil.getRichImg(infoOtherResp.account.richs)
            mBinding.richImg.setImageResource(richsImage)
            val (levelBack, _) = commonRich.second to "Lv${commonRich.first}"
            if (infoOtherResp.account.richs == 0L) {
                richDetail.visibility = View.GONE
            } else {
                richDetail.text = "${infoOtherResp.account.richs}"
            }
            if (levelBack == null) {
                if (CommonUtils.isMan(infoOtherResp.sex)) level1.visibility =
                    View.GONE else level2.visibility = View.GONE
                richLevel.text = "Lv.0"
            } else {
                if (CommonUtils.isMan(infoOtherResp.sex)) {
                    level1.setRichsLevel(infoOtherResp.account.richs)
                } else {
                    level2.setRichsLevel(infoOtherResp.account.richs)
                }
                richLevel.text = "Lv.${commonRich.first}"
            }
            mBinding.vipView.setVipLevel(infoOtherResp.profile.vipLevel)
            //魅力等级
            val commonCharm = RichCharmImgUtil.getCommonCharmImg(infoOtherResp.account.charms)
            val (_, _, charmsImage) = RichCharmImgUtil.getCharmImg(infoOtherResp.account.charms)
            mBinding.charmImg.setImageResource(charmsImage)
            val (levelCharmBack, _) = commonCharm.second to "Lv${commonCharm.first}"
            if (infoOtherResp.account.charms == 0L) {
                charmDetail.visibility = View.GONE
            } else {
                charmDetail.text = "${infoOtherResp.account.charms}"
            }
            if (levelCharmBack == null) {
                charmLevel.text = "Lv.0"
                if (CommonUtils.isMan(infoOtherResp.sex)) level2.visibility =
                    View.GONE else level1.visibility = View.GONE
            } else {
                if (CommonUtils.isMan(infoOtherResp.sex)) {
                    level2.setCharmsLevel(infoOtherResp.account.charms)
                } else {
                    level1.setCharmsLevel(infoOtherResp.account.charms)
                }
                charmLevel.text = "Lv.${commonCharm.first}"
            }

            if (TextUtils.isEmpty(infoOtherResp.profile.aboutMe)) {
                relAboutMe.visibility = View.GONE
            } else {
                aboutMeText.text = infoOtherResp.profile.aboutMe
            }

            giftRecycler.apply {
                adapter = mGiftAdapter
                addItemDecoration(CommonItemDecoration(rightMargin = ConvertUtils.dp2px(8f)))
                layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            }
            val list = mutableListOf<UserOtherGiftVo>()
            if (infoOtherResp.gifts.size < 4) {
                list.addAll(infoOtherResp.gifts)
                val num = 4 - list.size
                for (i in 0 until num) {
                    list.add(UserOtherGiftVo())
                }
                mGiftAdapter.setList(list)
            } else {
                mGiftAdapter.setList(infoOtherResp.gifts)
            }
            honorView.setData(infoOtherResp.medals, infoOtherResp.userId, mViewModel)
        }

        //封面照
        if (infoOtherResp.coverPhoto == null || infoOtherResp.coverPhoto.status != AppConst.CoverPhoto.APPROVED) {
            mBinding.llLike.visibility = View.GONE
            if (infoOtherResp.userId == UserManager.getUser().userId) {
//                mBinding.videoCard.visibility = View.GONE
            }
        } else {
            if (infoOtherResp.live?.liveStreamId != null) {
//                mBinding.videoCard.visibility = View.VISIBLE
            }
        }

        if (!CommonUtils.isMan(infoOtherResp.sex) && infoOtherResp.labels.isNotEmpty()) {
            val flexBoxLayout = FlexboxLayoutManager(mActivity)
            flexBoxLayout.flexDirection = FlexDirection.ROW
            flexBoxLayout.justifyContent = JustifyContent.FLEX_START
            flexBoxLayout.flexWrap = FlexWrap.WRAP

            //标签 或者说 印象
            mBinding.labelRecycler.apply {
                adapter = mLabelAdapter
                layoutManager = flexBoxLayout
            }
            mLabelAdapter.setList(infoOtherResp.labels)
        } else {
            mBinding.tvImpression.visibility = View.GONE
            mBinding.labelRecycler.visibility = View.GONE
        }

//        if (infoOtherResp.call?.actualPrice == 0L) {
//            mBinding.freeMatch.visibility = View.VISIBLE
//            mBinding.goCall.visibility = View.GONE
//        } else {
//            mBinding.goCall.visibility = View.VISIBLE
//            mBinding.freeMatch.visibility = View.GONE
//            mBinding.callCoin.text = "${infoOtherResp.call?.actualPrice}"
//            mBinding.ivPlay.apply {
//                path = USER_PROFILE_CALL_PAG
//                setRepeatCount(-1)
//                play()
//            }
//        }

        if (infoOtherResp.userId == UserManager.getUser().userId) {
            mySelfUi(infoOtherResp)
        } else {
            otherUi(infoOtherResp)
        }

        updateUserState(infoOtherResp)

        if (infoOtherResp.userId != UserManager.getUserId()) {
            mBinding.tvEmpty.visibility = View.GONE
        }
        mBinding.llFlag.setLabel(infoOtherResp.flag.label)
    }

    //个人中心的ui和赋值
    private fun mySelfUi(infoOtherResp: InfoOtherResp) {
        mBinding.apply {
            ViewUtils.setMargin(mBinding.scroll, bottomMargin = 0)
            mBinding.ivState.visibility = View.GONE
            icMore.setImageResource(R.drawable.ic_edit)
            llBottom.visibility = View.GONE
            followImg.visibility = View.GONE
            reMakeLl.visibility = View.GONE
            addLike.visibility = View.GONE
            //点赞
            val likeNum = CacheUtil.getLikeNum(infoOtherResp.userId.toString())
            mBinding.likeNum.text = likeNum.toString()

            val likeAdd = infoOtherResp.account.likes - likeNum

            if (likeAdd > 0) {
                if (mBinding.likeNumAdd.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
                    mBinding.likeNumAdd.text = "$likeAdd+"
                } else {
                    mBinding.likeNumAdd.text = "+$likeAdd"
                }
            }
            CacheUtil.setLastLikeNum(infoOtherResp.userId.toString(), infoOtherResp.account.likes)
            llLike.setOnSingleClickListener {
                Router.instance.setPath(RoutePath.USER_SPACE_LIKE_ACTIVITY).navigation(mActivity)
            }

            mGiftAdapter.setOnItemClickListener { _, _, position ->
                if (mGiftAdapter.data[position].url?.isNotEmpty() == true) {
                    Router.instance.setPath(RoutePath.GIFT_DETAIL_ACTIVITY)
                        .withLong(CommonConst.Gift.GIFT_ID, mGiftAdapter.data[position].giftId)
                        .withString(
                            CommonConst.Gift.GIFT_PIC,
                            mGiftAdapter.data[position].url ?: ""
                        )
                        .withString(
                            CommonConst.Gift.GIFT_NAME,
                            mGiftAdapter.data[position].giftName ?: ""
                        )
                        .navigation(mActivity)
                }
            }

            val layoutParams = llLike.layoutParams as ConstraintLayout.LayoutParams
            layoutParams.bottomMargin = 78.dp
            llLike.layoutParams = layoutParams
            //如果是付费用户或者是公会用可以上传
            val isShowPhoto = UserManager.getUser().profile?.ownerAgencyId != null
                    || UserManager.getUser().profile?.agencyId != null
                    || UserManager.getUser().flag?.recharged.toBoolean()

            if (!isShowPhoto) {
                ivEmptyBg.visibility = View.VISIBLE
                ivLogImg.visibility = View.VISIBLE
                if (CommonUtils.isMan(infoOtherResp.sex)) {
                    ivEmptyBg.setImageResource(R.drawable.me_title_back)
                } else {
                    ivEmptyBg.setImageResource(R.drawable.me_title_back)
                }
                val params = relTitle.layoutParams as ConstraintLayout.LayoutParams
                params.topMargin = 134.dp
                mBinding.relTitle.layoutParams = params
            } else {
                val list = mutableListOf<UserOtherPhotoVo>()
                list.addAll(infoOtherResp.photos)

                photoRecyclerview.visibility = View.VISIBLE
                photoRecyclerview.apply {
                    layoutManager = LinearLayoutManager(mActivity, RecyclerView.HORIZONTAL, false)
                    adapter = mPhotoAdapter
                }
                if (list.isEmpty()) {
                    ivEmpty.visibility = View.VISIBLE
                    tvEmpty.visibility = View.VISIBLE
                    ivEmpty.setImageResource(R.drawable.bg_me_photo_empty)
                } else {
                    clickView.visibility = View.GONE
                }
                initBannerViewPager(mPhotoAdapter, list.toList(), infoOtherResp)
                if (infoOtherResp.photos.size < 5) {
                    list.add(UserOtherPhotoVo(0, "", 0))
                }
                mPhotoAdapter.setList(list)
                mPhotoAdapter.setSelect(0)
                mPhotoAdapter.setOnItemChildClickListener { _, _, position ->
                    val data = mPhotoAdapter.data[position]
                    if (data.photoId == COVERPHOTOID && data.status == AppConst.CoverPhoto.APPROVED) {
                        Router.builder(RoutePath.SET_LIVE_IMAGE_ACTIVITY)
                            .withInt("type", LiveConst.Type.CLIENT_DEFAULT)
                            .withString("name", "")
                            .navigation(mActivity)
                        return@setOnItemChildClickListener
                    }
                    mViewModel.request(
                        { userService.deletePhoto(mPhotoAdapter.data[position].photoId) },
                        {
                            mPhotoAdapter.remove(mPhotoAdapter.data[position])
                            if (!mPhotoAdapter.data.contains(UserOtherPhotoVo(0, "", 0))) {
                                mPhotoAdapter.addData(UserOtherPhotoVo(0, "", 0))
                            }
                            mPhotoAdapter.clearSelected()
                            mPhotoAdapter.setSelect(0)
                            mBinding.bannerView.removeItem(position)
                            mBinding.bannerView.setCurrentItem(0, false)
                            if (mPhotoAdapter.itemCount == 1 && mPhotoAdapter.data.contains(
                                    UserOtherPhotoVo(0, "", 0)
                                )
                            ) {
                                clickView.visibility = View.VISIBLE
                                ivEmpty.visibility = View.VISIBLE
                                tvEmpty.visibility = View.VISIBLE
                            } else {
                                ivEmpty.visibility = View.GONE
                                tvEmpty.visibility = View.GONE
                            }
                        },
                        { ToastGravity.showGravity(it.errorMsg) })
                }
                mPhotoAdapter.setOnItemClickListener { _, _, position ->
                    if (position == mPhotoAdapter.data.size - 1 && mPhotoAdapter.data[position].url.isEmpty()) {
                        XPopup.Builder(mActivity)
                            .moveUpToKeyboard(false)
                            .isDestroyOnDismiss(true)
                            .asCustom(SelectImagePopup(mActivity, onSelectIt = { type ->
                                when (type) {
                                    SelectImagePopup.CAMERA -> {
                                        againCamera()
                                    }

                                    else -> {
                                        CropUtils.startAlbum(mActivity, {
                                            uploadPhoto(it, infoOtherResp)
                                        }, {})
                                    }
                                }
                            }))
                            .show()
                    } else {//点击不是添加图片按钮
                        mPhotoAdapter.clearSelected()
                        mPhotoAdapter.setSelect(position)
                        mBinding.bannerView.setCurrentItem(position, false)
                    }
                }
            }
            mBinding.clickView.setOnSingleClickListener {
                if (infoOtherResp.coverPhoto?.status != AppConst.CoverPhoto.APPROVED) return@setOnSingleClickListener

                XPopup.Builder(mActivity)
                    .moveUpToKeyboard(false)
                    .isDestroyOnDismiss(true)
                    .asCustom(SelectImagePopup(mActivity, onSelectIt = { type ->
                        when (type) {
                            SelectImagePopup.CAMERA -> {
                                againCamera()
                            }

                            else -> {
                                CropUtils.startAlbum(mActivity, {
                                    uploadPhoto(it, infoOtherResp)
                                }, {})
                            }
                        }
                    }))
                    .show()
            }
        }
    }

    fun uploadPhoto(realPath: String, infoOtherResp: InfoOtherResp) {
        mViewModel.showLoading(waitTime = 0)

        ImageUtils.s3Upload(
            realPath,
            AppConst.UpLoad.ALBUM,
            1024,
            callback = object : S3UploadCallback {
                override fun onSuccess(url: String, size: Pair<Int, Int>?) {
                    mViewModel.requestLoading(
                        { userService.uploadPhoto(UploadPhotoBean(url)) },
                        {
                            val addIndex = mPhotoAdapter.data.size - 1
                            if (mPhotoAdapter.data.size == 6 && mPhotoAdapter.data.contains(
                                    UserOtherPhotoVo(0, "", 0)
                                )
                            ) {
                                mPhotoAdapter.data[addIndex] = UserOtherPhotoVo(0, url, 0)
                                mPhotoAdapter.notifyItemChanged(addIndex)
                            } else {
                                mPhotoAdapter.addData(
                                    mPhotoAdapter.data.size - 1,
                                    UserOtherPhotoVo(0, url, 0)
                                )
                            }
                            initBannerViewPager(
                                mPhotoAdapter,
                                mPhotoAdapter.data.filter { it.url.isNotEmpty() }
                                    .toMutableList(), infoOtherResp)
                            mBinding.clickView.visibility = View.GONE
                        },
                        { ToastGravity.showGravity(it.errorMsg) })
                }

                override fun onFailure(t: Throwable) {
                    LogExt.logE("--net", t)

                    ToastGravity.showGravity(t.message)

                    LoadingUtils.dismissLoadingDialog()
                }
            })
    }

    //回到这个页面需要刷新的ui
    fun onResumeData(infoOtherResp: InfoOtherResp) {
        val isShowAvatar =
            UserManager.getUserId() == infoOtherResp.userId && UserManager.getUser().avatarModified == AppConst.NO
        if (isShowAvatar) {
            Glide.with(mActivity)
                .load(R.drawable.self_default_avatar)
                .normal()
                .into(mBinding.ivAvatar)
            mBinding.ivEdit.visibility = View.VISIBLE
        } else {
            Glide.with(mActivity)
                .load(ImageUtils.imgPathMiddle(infoOtherResp.avatar))
                .normal()
                .into(mBinding.ivAvatar)
            mBinding.ivEdit.visibility = View.GONE
        }
//        if (infoOtherResp.call?.actualPrice == 0L) {
//            mBinding.freeMatch.visibility = View.VISIBLE
//            mBinding.goCall.visibility = View.GONE
//        } else {
//            mBinding.goCall.visibility = View.VISIBLE
//            mBinding.freeMatch.visibility = View.GONE
//            mBinding.callCoin.text = "${infoOtherResp.call?.actualPrice}"
//            mBinding.ivPlay.apply {
//                path = USER_PROFILE_CALL_PAG
//                setRepeatCount(-1)
//                play()
//            }
//        }
    }

    private fun initBannerViewPager(
        mPhotoAdapter: MePhotoAdapter,
        data: List<UserOtherPhotoVo>,
        infoOtherResp: InfoOtherResp,
    ) {
        mBinding.bannerView.apply {
            registerLifecycleObserver(mActivity.lifecycle)
            setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorVisibility(View.GONE)
            setAdapter(mBannerAdapter)
            setOnPageClickListener { view, position ->
                PicturePreview.with(mActivity)
                    .byView(view)
                    .setImageUrlList(ArrayList(data.map { ImageUtils.imgPath(it.url) }))
                    .setIndex(position)
                    .setUserInfo(infoOtherResp)
                    .go()
            }
            setScrollDuration(500)
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                @SuppressLint("SetTextI18n")
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    mPhotoAdapter.clearSelected()
                    mPhotoAdapter.setSelect(position)
                }
            })
            create(data)
        }
    }

    //他人中心的ui和赋值
    @SuppressLint("SetTextI18n")
    private fun otherUi(infoOtherResp: InfoOtherResp) {
        mBinding.apply {
            val layout = scroll.layoutParams as RelativeLayout.LayoutParams
            layout.bottomMargin = 80.dp
            scroll.layoutParams = layout
            llLike.visibility = View.GONE
            charmDetail.visibility = View.GONE
            richDetail.visibility = View.GONE
            clickView.visibility = View.GONE
            mBinding.likeNum.text = infoOtherResp.account.likes.toString()
            if (infoOtherResp.remark?.remark?.isNotEmpty() == true) {
                selectRemark.text = infoOtherResp.remark.remark
            }
            if (infoOtherResp.relation?.followed == true) {
                followImg.visibility = View.GONE
            }
            val filteredList = infoOtherResp.photos.filter { it.status == YES }
            if (filteredList.isEmpty()) { //没有通过审核的照片
                addLike.visibility = View.GONE
                ivEmptyBg.visibility = View.VISIBLE
                ivLogImg.visibility = View.VISIBLE
                if (CommonUtils.isMan(infoOtherResp.sex)) {
                    ivEmptyBg.setImageResource(R.drawable.me_title_back)
                } else {
                    ivEmptyBg.setImageResource(R.drawable.me_title_back)
                }
                val params = relTitle.layoutParams as ConstraintLayout.LayoutParams
                params.topMargin = 134.dp
                mBinding.relTitle.layoutParams = params
            } else {
                if (infoOtherResp.relation?.liked == true) {
                    addLike.visibility = View.GONE
                    llLike.visibility = View.VISIBLE
                } else {
                    addLike.apply {
                        path = LIKE_BEFORE_PAG
                        setRepeatCount(-1)
                        play()
                    }
                }
                val photos = ArrayList<UserOtherPhotoVo>().apply {
                    if (infoOtherResp.coverPhoto != null && infoOtherResp.coverPhoto.status == AppConst.CoverPhoto.APPROVED) {
                        add(
                            UserOtherPhotoVo(
                                COVERPHOTOID,
                                infoOtherResp.coverPhoto.url,
                                infoOtherResp.coverPhoto.currentStatus ?: 1
                            )
                        )
                    }
                    addAll(infoOtherResp.photos)
                }

                if (photos.isEmpty()) {
                    ivEmpty.visibility = View.VISIBLE
                    tvEmpty.visibility = View.VISIBLE
                    tvBanner.visibility = View.GONE
                    ivEmpty.setImageResource(R.drawable.bg_other_photo_empty)
                } else {
                    tvBanner.visibility = View.VISIBLE
                    tvBanner.text = "1/${photos.size}"

                }
                bannerView.apply {
                    registerLifecycleObserver(mActivity.lifecycle)
                    setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
                    setIndicatorVisibility(View.GONE)
                    setAdapter(mBannerAdapter)
                    setOnPageClickListener { view, position ->
                        PicturePreview.with(mActivity)
                            .byView(view)
                            .setImageUrlList(ArrayList(photos.map { ImageUtils.imgPath(it.url) }))
                            .setIndex(position)
                            .setUserInfo(infoOtherResp)
                            .go()
                    }
                    setScrollDuration(500)
                    registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                        @SuppressLint("SetTextI18n")
                        override fun onPageSelected(position: Int) {
                            super.onPageSelected(position)
                            tvBanner.text = "${position + 1}/${photos.size}"
                        }
                    })
                    create(photos)
                }
            }

        }
    }

    private fun updateUserState(infoOtherResp: InfoOtherResp) {
        updateUserState(
            UserStateUtils.resolveUserState(infoOtherResp),
            infoOtherResp.live?.stateBanner
        )
    }

    fun updateUserState(userState: UserState) {
        updateUserState(UserStateUtils.resolveUserState(userState), userState.stateBanner)
    }

    private fun updateUserState(@UserStatus state: Int, stateBanner: String? = null) {
        if (state == UserStateUtils.USER_STATE_ONLINE || state == UserStateUtils.USER_STATE_OFFLINE || state == UserStateUtils.USER_STATE_BUSY) {
            mBinding.vState.visibility = View.VISIBLE
            mBinding.ivState.visibility = View.GONE
        } else {
            mBinding.vState.visibility = View.GONE
            mBinding.ivState.visibility = View.VISIBLE
        }
        mBinding.consUserParty.visibility = View.GONE
        when (state) {
            UserStateUtils.USER_STATE_ONLINE -> {
                mBinding.vState.setBackgroundResource(R.drawable.ic_user_space)
            }

            UserStateUtils.USER_STATE_OFFLINE -> {
                mBinding.vState.setBackgroundResource(R.drawable.shape_session_state_offline)
            }

            UserStateUtils.USER_STATE_BUSY -> {
                mBinding.vState.setBackgroundResource(R.drawable.ic_user_space)
//                mBinding.vState.setBackgroundResource(R.drawable.shape_session_state_busy)
            }

            UserStateUtils.USER_STATE_LIVE -> {
                mBinding.vState.setBackgroundResource(R.drawable.ic_user_space)
//                mBinding.ivState.setImageResource(null)
            }

            UserStateUtils.USER_STATE_MATCH -> {
                mBinding.vState.setBackgroundResource(R.drawable.ic_user_space)
//                mBinding.ivState.setImageResource(R.drawable.icon_list_match)
            }

            UserStateUtils.USER_STATE_PARTY, UserStateUtils.USER_STATE_PARTY_VIDEO -> {
                mBinding.ivState.setImageResource(R.drawable.icon_party)
            }

            UserStateUtils.USER_STATE_GAME, UserStateUtils.USER_GAME_WAITING, UserStateUtils.USER_GAME_PLAYING -> {
                mBinding.ivState.setImageResource(R.drawable.icon_list_gameing)
            }

            else -> {
                mBinding.vState.visibility = View.GONE
                mBinding.ivState.visibility = View.GONE
            }
        }
        stateBanner?.let {
            mBinding.consUserParty.visibility = View.VISIBLE
            Glide.with(mActivity).load(ImageUtils.imgPath(it))
                .into(mBinding.ivPartyBack)
        }
    }

}